<template>
    <realTimeData v-if="isLoad" :cfg="cfg" @reLoad="reLoad" @saveConfig="saveConfig"></realTimeData>
</template>


<script>

import elementui from '~/plugins/element-ui';
import exfnInit from '~/plugins/exfnInit';
import Vue from "vue";
import realTimeData from "./realTimeData.vue";

export default {
    layout: 'default',
    components:{
        realTimeData,
    },
    data() {
        return {
            isLoad: false,
            cfg :{
                amountUnit: 1,
                "sleepTime" : 3000,
                "diffTime" : 60,
                "wave": 1,
                "event":{
                    "chart_time_num":true,
                    chart_time_num_min: 100,
                    chart_time_num_bet: 2.5,
                    "chart_time_range":true,
                    chart_time_range_minute: 20,
                    main_money:true,
                    gt_money:true,
                    detail_wave: true,
                    detail_num: true,
                    detail_num_low: 500,
                    detail_num_high: 100,

                    detail_newpoint: true,
                    detail_dfcf:true,
                    detail_zt_num:true,
                    detail_zt_num_diff: 500,
                },
                kplMode: false,
                "jjList":[
                    {"name":"中小","code":"110011","rate":""},
                    {"name":"信息","code":"001513","rate":""}
                ],
                "bkList":[
                    {"name":"医疗行业","code":"BK0727","rate":""},
                    {"name":"次新股","code":"BK0501","rate":""}
                ],
                "favList":[
                    {"c":"300869","n":"康泰医学","p":"","r":"","watch":true,"jjzb":"0"},
                    {"c":"300888","n":"稳健医疗","p":"","r":"","watch":true,"jjzb":"0"},
                    {"c":"300863","n":"卡倍亿","p":"","r":"","watch":true,"jjzb":"0"},
                    {"c":"300318","n":"博晖创新","p":"","r":"","watch":true,"jjzb":"0"},
                    {"c":"002271","n":"东方雨虹","p":"","r":"","watch":false,"jjzb":"0"},
                    {"c":"300881","n":"铂科新材","p":"","r":"","watch":false,"jjzb":"0"}
                ],
                // "db":{
                //     "host"     : 'localhost',
                //     "port"     : "3306",
                //     "user"     : 'root',
                //     "password" : 'root',
                //     "database" : 'books'
                // },
                "account": ""
            }
        }
    },

    mounted() {
        this.listenKpl();
        this.reLoad();
    },

    methods: {
        listenKpl(){
            if(!window.electron){
                return;
            }
            var that = this;
            electron.ipcRenderer.on('setKplListRes', (event, list) => {
                if(!that.cfg.kplMode) return;
                if(!list || !list.length || list.length==0) return;
                var curcodes = "";
                that.cfg.favList.map(item=>{curcodes+=item.c})
                var newcodes = "";
                list.map(item=>{newcodes+=item.c})
                if(curcodes==newcodes){
                    that.cfg.favList.map((item,idx)=>{
                        Object.assign(item, list[idx]);
                    })
                }else{
                    that.cfg.favList = list;
                }
                // Object.assign(that.cfg.favList, list);
            })
            
        },
        saveConfig(){
            this.$confirm('确认要保存记录？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var that = this;
                
                this.registerListen(this,"saveConfig",(err)=>{
                    try{
                        if(err){
                            that.$message.error("配置保存异常:"+err)
                        }else{
                            that.$message.success("保存成功")
                        }
                    }catch(ex){
                            that.$message.error("配置保存异常:"+err)
                    }
                });
                
                electron.ipcRenderer.send("saveConfig",this.cfg)
            });
        },
        reLoad(){
            var that = this;
            if(!window.electron){
                that.isLoad = true;
                return;
            }
            this.registerListen(this,"setConfig",(cfg)=>{
                try{
                    // console.info("loadCfg:" ,cfg)
                    Object.assign(that.cfg, cfg);
                    that.isLoad = true;
                }catch(ex){
                    that.$message.error("配置加载异常:"+ex.message)
                }
            });
            electron.ipcRenderer.send("getConfig","")

        },
    }
    
}
</script>

<style lang="less">

</style>
